package databinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import model.ChiTietTacGia_Sach;
import model.DichGia;
import model.NhaXuatBan;
import model.Sach;
import model.TacGia;
import model.TheLoai;

import org.hibernate.criterion.Restrictions;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;

import controller.ShoppingCart;
import dao.entity.DichGiaDAO;
import dao.entity.NhaXuatBanDAO;
import dao.entity.SachDAO;
import dao.entity.TacGiaDAO;
import dao.entity.TheLoaiDAO;

public class SachViewModel extends SelectorComposer<Component> {
	private static final long serialVersionUID = 1L;
	SachDAO sach = new SachDAO();
	ChiTietTacGia_Sach tg_sach = new ChiTietTacGia_Sach();
	TheLoaiDAO theLoai = new TheLoaiDAO();
	TacGiaDAO tacGia = new TacGiaDAO();
	DichGiaDAO dichGia = new DichGiaDAO();
	NhaXuatBanDAO nhaXuatBanDAO = new NhaXuatBanDAO();
	List<Sach> listSach = sach.selectAll();
	List<TheLoai> listTheLoai = theLoai.selectAll();
	List<TacGia> listTacGia = tacGia.selectAll();
	List<DichGia> listDichGia = dichGia.selectAll();
	List<NhaXuatBan> listNhaXuatBan = nhaXuatBanDAO.selectAll();

	@Init
	public List<Sach> getListSach() {
		return listSach;
	}

	public String[] getListTenSach() {
		String[] list = new String[getListSach().size()];
		for (int i = 0; i < getListSach().size(); i++) {
			list[i] = getListSach().get(i).getTenSach();
		}
		return list;

	}

	public List<TheLoai> getTheLoai() {
		return listTheLoai;
	}

	public List<String> getTacGia() {
		List<String> listTenTacGia = new ArrayList<String>();
		for (int i = 0; i < listTacGia.size(); i++) {
			listTenTacGia.add(listTacGia.get(i).getTenTacGia());
		}
		return listTenTacGia;
	}

	public List<String> getDichGia() {
		List<String> listTenDichGia = new ArrayList<String>();
		for (int i = 0; i < listDichGia.size(); i++) {
			listTenDichGia.add(listDichGia.get(i).getTen_DichGia());
		}
		return listTenDichGia;
	}

	public List<String> getNhaXuatBan() {
		List<String> listTenNhaXuatBan = new ArrayList<String>();
		for (int i = 0; i < listNhaXuatBan.size(); i++) {
			listTenNhaXuatBan.add(listNhaXuatBan.get(i).getTenNXB());
		}
		return listTenNhaXuatBan;
	}

	// Lấy id sách khi click chuột
	@Command
	public void bookDetail(@BindingParam("id") Sach sach) {
		Sessions.getCurrent().setAttribute("sach", sach);
		Executions.sendRedirect("bookdetail.zul");

	}

	@Wire
	private Combobox combo;

	@Listen("onClick=#timkiem")
	public void timKiem() {
		System.out.println(combo.getValue());
		List<Sach> list = sach.findByCriteria(Restrictions.eq("tenSach",
				combo.getValue()));
		if (list.size() == 0) {
			Executions.sendRedirect("notfound.zul");

		} else {
			Sessions.getCurrent().setAttribute("sach", list.get(0));
			Executions.sendRedirect("bookdetail.zul");
		}
	}
	@Wire
	private Combobox combo1;
	@Listen("onClick=#timkiemSach")
//	public void timKiemSach() {
//		List<ChiTietTacGia_Sach> list = tg_sach.findByCriteria(Restrictions.eq("tenSach",
//				combo1.getValue()));
//		if (list.size() == 0) {
//			list = null;
//			Sessions.getCurrent().setAttribute("list", list);
//		} else {
//			Sessions.getCurrent().setAttribute("list", list);
//			Executions.sendRedirect("");
//		}
//	}

	public List<Sach> getListProduct() {
		ProductViewModel product = new ProductViewModel();
		List<Sach> list = product.getlistSach();
		return list;
	}

	// Lấy đối tượng thể loại khi click vào tên thể loại
	@Command
	public void theLoai(@BindingParam("theloai") TheLoai theloai) {
		Sessions.getCurrent().setAttribute("theloai", theloai);
		System.out.println(theloai.getTenTheLoai());
		Executions
				.sendRedirect("product.zul?target=" + theloai.getId_TheLoai());

	}

	@Command
	public void loadAgainPage() {
		Executions.sendRedirect("addbook.zul");
	}

	ShoppingCart shoppingCart = new ShoppingCart();
	List<Sach> listSachCart = new ArrayList<>();

	HashMap<Integer, Sach> hashMapGioHang = new HashMap<Integer, Sach>();

	// them vao gio hang
	@SuppressWarnings("unused")
	@Command
	public void addGioHang(@BindingParam("id") Sach sach) {
		Integer idSach = sach.getId_Sach();
		String tenSach = sach.getTenSach();
		@SuppressWarnings("unchecked")
		HashMap<Integer, Sach> hashMapGioHang = (HashMap<Integer, Sach>) Sessions
				.getCurrent().getAttribute("gioHang");
		if (hashMapGioHang == null) {
			hashMapGioHang = new HashMap<Integer, Sach>();
			hashMapGioHang.put(idSach, sach);

		} else {
			hashMapGioHang.put(idSach, sach);
		}

		// Chuyển trang truyền tham số
		Sessions.getCurrent().setAttribute("gioHang", hashMapGioHang);
		Executions.sendRedirect("shoppingcart.zul?target=chitietgiohang");
	}

	// List<Sach> listSachCart = new ArrayList<>();
	Sach sachCart = new Sach();

	// them vao gio hang

}
